package com.no_spring.hellokafka;


import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

//hello world版本 kafka生产者
public class S1 {
    public static void main(String[] args) {
        //生产者三个属性必须：broker-地址清单，key和value的序列化器
        Properties props = new Properties();
        props.put("bootstrap.servers","127.0.0.1:9092");
        props.put("key.serializer","org.apache.kafka.common.serialization.StringSerializer");
        props.put("value.serializer","org.apache.kafka.common.serialization.StringSerializer");

        KafkaProducer<String,String> producer = new KafkaProducer<String, String>(props);

        try{
            ProducerRecord<String,String> record;
            try{
                for(int i=0;i<3;i++){
                    String msg = "mark:"+(i+100);
                    record = new ProducerRecord<String, String>("topic1",null,msg);
                    producer.send(record);
                    System.out.println(i+" "+msg+" is sent");
                }
            }catch (Exception e){
                e.printStackTrace();
            }

        }finally {
            producer.close();
        }
    }
}
